<?php

class hotel_instances_you {



// FUNCTION // lookup hotel based on You Travel instance values. Return hotel values if true.
function lookup_hotel($params=''){

	$params = in($params);

	if(!empty($params['supplier_code'])){
	
		
		$sql = "SELECT *, H.tree_id as hotel_id
		FROM tree AS H
		INNER JOIN hotel_instances_youtravel AS I on H.tree_id = I.tree_id
		WHERE 1 = 1 
		AND I.hotel_id_youtravel = '".$params['hotel_id_youtravel']."'
		LIMIT 1	";	

		
		return apply_query($sql);
			
	}
	
}





// FUNCTION // find a hotel instances that are linked to the hotels table
function search_hotel_instances($params){

// use safe function
$params = in($params);

	if($params['tree_id']) $where .= " AND tree_id = '".$params['tree_id']."' ";

	if(isset($where)){
	
		$sql = "SELECT *
		FROM hotel_instances_youtravel
		WHERE 1 = 1 
		".$where."
		ORDER by date_linked
		LIMIT 1 "; 
	
		return apply_query($sql);
		
	}else{
		return false;
	}

}



// FUNCTION // search to see if an instance is present in the flagged table
function lookup_flagged_instance($params){


	$params = in($params);
	
    $sql = "SELECT hotel_name
    FROM hotel_instances_youtravel_flagged
    WHERE hotel_name = '".$params['hotel_name']."'
	AND hotel_id_youtravel = '".$params['hotel_id_youtravel']."'
	LIMIT 1	";	
	
	return apply_query($sql);
	
}




// FUNCTION // Insert instance values into the flagged table
function flag_not_found($params){

	if(!empty($params)){

		$params = in($params);
		
		$sql = "INSERT INTO hotel_instances_youtravel_flagged
		SET hotel_name = '".$params['hotel_name']."', 
		hotel_id_youtravel = '".$params['hotel_id_youtravel']."',
		youtravel_rating = '".$params['youtravel_rating']."',
		official_rating = '".$params['official_rating']."',
		board_type = '".$params['board_type']."',
		hotel_description = '".$params['hotel_description']."',
		hotel_photos = '".$params['hotel_photos']."',
		country = '".$params['country']."',
		destination = '".$params['destination']."',
		resort = '".$params['resort']."',
		instance_date = '".mktime()."' ";
	
		$result = mysql_query($sql) or die (mysql_error());	

	}

}









// FUNCTION // find instances that are in the flagged table
function search_flagged_instances($params){
	

	$params = in($params);	

	if($params['prefix']) $where .= " AND UPPER(LEFT(hotel_name, 1)) = '".$params['prefix']."' ";
	if($params['name']) $where .= " AND hotel_name LIKE '%".$params['name']."%' ";

    $sql = "SELECT *
    FROM hotel_instances_youtravel_flagged
    WHERE 1 = 1 
	".$where."
    ORDER by instance_date"; 


	return apply_query($sql);

}





// FUNCTION // get a single instance from the flagged table
function get_flagged_instance($params){


	$params = in($params);

	if(!empty($params)){
	
		$sql = "SELECT *
		FROM hotel_instances_youtravel_flagged
		WHERE instance_id = '".$params['instance_id']."'
		LIMIT 1	";
		
		$result = mysql_query($sql) or die (mysql_error());
		
		$row = mysql_fetch_array($result);
	
		return $row;
		
	}
	
}






// FUNCTION // Insert a linked instance into the instance table
function link_instance($params=''){

	$instance_values = array();
	
	$ins_arg['instance_id'] = $params['instance_id'];
	$instance_values = $this->get_flagged_instance($ins_arg);
	
	$instance_values['tree_id'] = $params['tree_id'];

	$this->insert_linked_instance($instance_values);

	$this->deleted_flagged_instance($params['instance_id']);
	
}







// FUNCTION // search to see if a record has been entered in the table
function insert_linked_instance($params){

	if(!empty($params)){
		
		$params = in($params);
	
		$sql = "INSERT INTO hotel_instances_youtravel
		SET tree_id = '".$params['tree_id']."',
		hotel_name = '".$params['hotel_name']."',
		hotel_id_youtravel = '".$params['hotel_id_youtravel']."',
		date_linked = '".mktime()."' ";
	
		$result = mysql_query($sql) or die (mysql_error());	
		
	}


}





// FUNCTION // Delete a flagged instance
function deleted_flagged_instance($instance_id=''){

	if(!empty($instance_id)){
		
		$sql = "DELETE FROM hotel_instances_youtravel_flagged
		WHERE instance_id = '".$instance_id."' ";
		
		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}




// FUNCTION // number of all accommodation
function number_flagged_instances(){

    $sql = "SELECT COUNT( instance_id ) AS count, instance_date
	FROM hotel_instances_youtravel_flagged
	ORDER BY instance_date"; 

    
	$result = mysql_query($sql) or die (mysql_error());
	
	$row = mysql_fetch_array($result);

	return $row;

}



/* Delete all instances attached to a tree ID. Should only be 1 tree ID as its a unique feild */
function delete_instances_by_tree($tree_id){

	if(!empty($tree_id)){
		
		$sql = "DELETE FROM hotel_instances_youtravel
		WHERE tree_id = '".$tree_id."' ";

		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}




}

?>